package Test4;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class SoccerLeague {
	
	public static void main(String[] args) {
		Set<Team> setTeam = new TreeSet<Team>();
		
		Team arsenal = new Team("Arsenal");
		arsenal.setDraw(5); arsenal.setWin(8); arsenal.setLose(4); 
		arsenal.setGoal(27); arsenal.setCon(13);
		setTeam.add(arsenal);
		Team manUtd = new Team("Man Utd");
		manUtd.setDraw(2); manUtd.setWin(14); manUtd.setLose(1); 
		manUtd.setGoal(38); manUtd.setCon(9);
		setTeam.add(manUtd);
		Team chelsea = new Team("Chelsea");
		chelsea.setDraw(3); chelsea.setWin(12); chelsea.setLose(2); 
		chelsea.setGoal(27); chelsea.setCon(9);
		setTeam.add(chelsea);
		Team portsmouth = new Team("Portsmouth");
		portsmouth.setDraw(4); portsmouth.setWin(8); portsmouth.setLose(5); 
		portsmouth.setGoal(23); portsmouth.setCon(14);
		setTeam.add(portsmouth);
		Team liverpool = new Team("Liverpool");
		liverpool.setDraw(4); liverpool.setWin(8); liverpool.setLose(5); 
		liverpool.setGoal(23); liverpool.setCon(15);
		setTeam.add(liverpool);
		Team tottenham= new Team("Tottenham");
		tottenham.setDraw(4); tottenham.setWin(7); tottenham.setLose(6); 
		tottenham.setGoal(20); tottenham.setCon(21);
		setTeam.add(tottenham);
		Team bolton = new Team("Bolton");
		bolton.setDraw(3); bolton.setWin(8); bolton.setLose(6); 
		bolton.setGoal(19); bolton.setCon(15);
		setTeam.add(bolton);
		Team reading = new Team("Reading");
		reading.setDraw(2); reading.setWin(8); reading.setLose(7); 
		reading.setGoal(19); reading.setCon(21);
		setTeam.add(reading);
		Team aston = new Team("Aston Villa");
		aston.setDraw(10); aston.setWin(5); aston.setLose(2); 
		aston.setGoal(21); aston.setCon(17);
		setTeam.add(aston);
		
		printAllTeam(setTeam);
		
		//memetakan antara no urut dg nama team
		Map<Integer,Team> maps = new LinkedHashMap<Integer, Team>();
		Iterator it = setTeam.iterator();
		int i=1;
		while(it.hasNext()){
			maps.put(i,(Team) it.next());
			i++;
		}
			
		int t1=0,t2=0;
		do{
			try {
				t1 = Integer.parseInt(getInput("Choose the first team: "));
			} catch (Exception e) {
				//System.out.println("You choose the wrong number. Team not found.");
			}
			try {
				t2 = Integer.parseInt(getInput("Choose the second team: "));
			} catch (Exception e) {
				//System.out.println("You choose the wrong number. Team not found.");
			}
			
			if(t1!=0 || t2!=0){
				Team team1 = maps.get(t1);
				Team team2 = maps.get(t2);
				
				if(team1==null || team2==null){
					System.out.println("You choose the wrong number. Team not found.");
				}
				else{
					String score=null;
					try {
						score = getInput("Enter the match result ("+team1.getNama()+"-"+team2.getNama()+") : ");
					} catch (Exception e) {
						System.out.println("Error : "+e.getMessage());
					}
					
					insertNewMatch(team1,team2,score);
					
					System.out.println("");
					printCurrentStanding(setTeam);
					team1.setLastPlay(false);
					team2.setLastPlay(false);
				}
			}
		}while(t1!=0 || t2!=0);
	}
	
	/**
	 *  getInput method 
	 */
	public static String getInput(String statement) throws Exception{
		System.out.print(statement);
		InputStreamReader is = new InputStreamReader(System.in);
		
		BufferedReader buffer = new BufferedReader(is);
		StringBuilder text = new StringBuilder();
		try {
			text.append(buffer.readLine()); //menyimpan ketika kita mengetik
		} catch (IOException e1) {
			System.out.println("Error : "+e1.getMessage());
		}
		return text.toString();
	}
	
	public static void printAllTeam(Set<Team> setTeam){
		Iterator it = setTeam.iterator();
		int i=1;
		System.out.println("0. (Exit)");
		while(it.hasNext()){
			System.out.print(i+". ");
			System.out.println(((Team)it.next()).getNama());
			i++;
		}
	}
	
	public static void printCurrentStanding(Set<Team> setTeam){
		System.out.println("Here are the current standings:");
		System.out.println("Club Name\tMatch Win   Draw  Lose  Goals-Con Point");
		List<Team> list = new ArrayList<Team>();  
		Iterator it = setTeam.iterator();
		while(it.hasNext()){
			Team team = (Team) it.next();
			team.countPoint();
			list.add(team);
		}
		Collections.sort(list,TeamComparator.byPoint());
		
		it = list.iterator();
		while(it.hasNext()){
			System.out.println(it.next().toString());
		}
	}
	
	public static void insertNewMatch(Team team1,Team team2,String score){
		StringTokenizer st = new StringTokenizer(score,"-");
		int[] scoreArr = new int[2];
		int i=0;
		while(st.hasMoreElements()){
			try{
				scoreArr[i] = Integer.parseInt((String) st.nextElement());
				i++;
			}
			catch(Exception e){
				System.out.println("Type with the correct format.");
				System.exit(i);
			}
		}
		
		team1.setGoal(team1.getGoal()+scoreArr[0]); 
		team1.setCon(team1.getCon()+scoreArr[1]);
		team2.setGoal(team2.getGoal()+scoreArr[1]);
		team2.setCon(team2.getCon()+scoreArr[0]);
		if(scoreArr[0]>scoreArr[1]){
			int now = team1.getWin();
			team1.setWin(team1.getWin()+1);
			team2.setLose(team2.getLose()+1);
		}
		else if(scoreArr[0]<scoreArr[1]){
			team2.setWin(team2.getWin()+1);
			team1.setLose(team1.getLose()+1);
		}
		else{
			team2.setDraw(team2.getDraw()+1);
			team1.setDraw(team1.getDraw()+1);
		}
		team1.countPoint(); team2.countPoint();
		team1.setLastPlay(true);
		team2.setLastPlay(true);
	}

}
